package com.sevenyear.excelanalyze.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.entity.param.entityParam.PartitionBaseInfoParam;
import com.sevenyear.excelanalyze.entity.param.queryConditionParam.PartitionBaseInfoQueryCondition;
import com.sevenyear.excelanalyze.listener.excel.PartitionBaseInfoListener;
import com.sevenyear.excelanalyze.mapper.PartitionBaseInfoMapper;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 分区基本信息 服务实现类
 * </p>
 *
 * @author sevenyear
 * @since 2021-08-20
 */
@Service
public class PartitionBaseInfoServiceImpl extends ServiceImpl<PartitionBaseInfoMapper, PartitionBaseInfo> implements PartitionBaseInfoService {

    @Override
    public void insertPartitionByExcel(MultipartFile file, PartitionBaseInfoService partitionBaseInfoService) {
        InputStream in = null;
        try {
            in = file.getInputStream();
            EasyExcel.read(in, PartitionBaseInfoParam.class, new PartitionBaseInfoListener(partitionBaseInfoService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void queryPageByCondition(Page<PartitionBaseInfo> pagePartition, PartitionBaseInfoQueryCondition partitionBaseInfoQueryCondition) {

        //构造条件
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();

        if (partitionBaseInfoQueryCondition == null){
            baseMapper.selectPage(pagePartition,wrapper);
            return;
        }
        //多条件组合查询
        String landform = partitionBaseInfoQueryCondition.getLandform();
        String clayThickness = partitionBaseInfoQueryCondition.getClayThickness();
        if (!StringUtils.isEmpty(landform)){
            wrapper.eq("landform",landform);
        }
        if (!StringUtils.isEmpty(clayThickness)){
            wrapper.eq("clay_thickness",clayThickness);
        }
        //调用方法实现条件查询分页
        baseMapper.selectPage(pagePartition,wrapper);

    }

    @Override
    public  List<PartitionBaseInfo> queryAllLandform() {
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT landform");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<PartitionBaseInfo> queryAllClayThickness() {
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT clay_thickness");
        return baseMapper.selectList(wrapper);
    }
}
